<?php

namespace app\admin\controller;

use think\Db;

class Admin extends Authorized
{

    public function index()
    {
        return $this->fetch();
    }


    public function getList()
    {
        $get_data=$this->request->get();
        $post_data = $this->request->post();

        $page = $get_data['page'];
        $limit = $get_data['limit'];

        $keywords = input('keywords');
        $where=[];
        if (!empty($keywords)){
            $where['tb_admin.name']=['like','%'.$keywords.'%'];
        }



        $count=Db::table('tb_admin')->where($where)->count();

        $rows=Db::table('tb_admin a')
            ->field('tb_admin.*,d.name depart_name,l.name level_name')
            ->join('tb_depart d','d.id=a.departID','left')
            ->join('tb_level l','l.id= a.levelid','left')
            ->where('a.id','>','1')
            ->where($where)
            ->order('state,levelid desc,id desc')
            ->page($page, $limit)
            ->select();

        return json(['code' =>0,'count' =>$count,'msg' =>'success','data'=>$rows]);
    }

    public function add()
    {
        $post_data = $this->request->post();
        if($post_data){

            $level=Db::table('tb_level')->field('num')->where('id',$post_data['levelid'])->find();
            $post_data['levelNum']=$level['num'];


            $post_data['pwd'] = substr(md5('siwei888'), 8, 16);
            Db::table('tb_admin')->startTrans();
            $id =Db::table('tb_admin')->insertGetId($post_data);
            if (false === $id) {
                Db::table('tb_admin')->rollback();
                return json(['code' =>1,'count' =>0,'msg' =>'添加失败','data'=>'']);
            } else {
               Db::table('tb_admin')->commit();
                return json(['code' =>0,'count' =>0,'msg' =>'添加成功','data'=>'']);
            }
        }

        $depart_list =Db::table('tb_depart')->order('id desc')->select();
        $level_list =Db::table('tb_level')->select();
        $this->assign('depart_list', $depart_list);
        $this->assign('level_list', $level_list);

        return $this->fetch();
    }

    public function reset()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=array_shift($post_data);
            $post_data['pwd'] = substr(md5('siwei888'), 8, 16);
            $result =Db::table('tb_admin')->where('id',$id)->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }
    }

    public function edit()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=array_shift($post_data);
            $level=Db::table('tb_level')->field('num')->where('id',$post_data['levelid'])->find();
            $post_data['levelNum']=$level['num'];
            $result =Db::table('tb_admin')->where('id',$id)->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }


        $article=Db::table('tb_admin a')->field('tb_admin.*,d.name depart_name,l.name level_name')
            ->join('tb_depart d','d.id= a.departID')
            ->join('tb_level l','l.id= a.levelid')
            ->where('a.id' , input('id'))
            ->find();
        $this->assign('article', $article);

        $depart_list =Db::table('tb_depart')->order('id desc')->select();
        $level_list =Db::table('tb_level')->select();
        $this->assign('depart_list', $depart_list);
        $this->assign('level_list', $level_list);


       return $this->fetch();
    }

    public function state()
    {
        $post_data = $this->request->post();
        if($post_data){
            $id=array_shift($post_data);
            $result =Db::table('tb_admin')->where('id',$id)->setField($post_data);
            if (false === $result) {
                return json(['msg' =>'失败', 'code' =>1]);
            } else {
                return json(['msg' =>'成功', 'code' =>0]);
            }
        }
    }


    public function del()
    {
        $post_data = $this->request->post();
        if($post_data){
            $result =Db::table('tb_admin')->where('id', $post_data['id'])->delete();
            if (false === $result) {
                return json(['msg' =>'删除失败', 'code' =>1]);
            } else {
                return json(['msg' =>'删除成功', 'code' =>0]);
            }
        }
    }
}